package com.ky.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ky.tms.entity.TmsOil;
import com.ky.tms.entity.TmsOilRechange;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.mapper.TmsOilMapper;
import com.ky.tms.mapper.TmsOilRechangeMapper;
import com.ky.tms.service.ITmsOilService;
import com.ky.tms.util.RequestContextHolderUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2020-05-13
 */
@Service
public class TmsOilServiceImpl extends ServiceImpl<TmsOilMapper, TmsOil> implements ITmsOilService {

    @Autowired
    private TmsOilMapper tmsOilMapper;

    @Autowired
    private TmsOilRechangeMapper tmsOilRechangeMapper;

    @Override
    public IPage<TmsOil> page(TmsOil tmsOil, Long limit, Long pages) {
        IPage<TmsOil> page = new Page(pages, limit);
        QueryWrapper<TmsOil> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tmsOil.getOilNo())){
            wrapper.like("oil_no", tmsOil.getOilNo());
        }
        wrapper.eq("is_valid", 1);
        wrapper.orderByDesc("create_time");
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public void add(TmsOil tmsOil) throws PredictableException {
        QueryWrapper<TmsOil> wrapper = new QueryWrapper<>();
        wrapper.eq("oil_no", tmsOil.getOilNo());
        wrapper.eq("is_valid", 1);
        Integer integer = tmsOilMapper.selectCount(wrapper);
        if(integer>0){
            throw new PredictableException("油卡号重复");
        }
        String userName = RequestContextHolderUtil.getUserName();
        tmsOil.setIsLock(0);
        tmsOil.setIsValid(1);
        tmsOil.setCreateTime(LocalDateTime.now());
        tmsOil.setCreateUser(userName);
        tmsOilMapper.insert(tmsOil);
    }

    @Override
    public void delete(List<Long> ids) {
        /*QueryWrapper<TmsOil> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        tmsOilMapper.delete(queryWrapper);*/
        String userName = RequestContextHolderUtil.getUserName();
        tmsOilMapper.deleteByIds(userName, LocalDateTime.now(), ids);
    }

    @Override
    public void update(TmsOil tmsOil) throws PredictableException {
        QueryWrapper<TmsOil> wrapper = new QueryWrapper<>();
        wrapper.eq("oil_no", tmsOil.getOilNo());
        wrapper.eq("is_valid", 1);
        wrapper.ne("id", tmsOil.getId());
        Integer integer = tmsOilMapper.selectCount(wrapper);
        if(integer>0){
            throw new PredictableException("油卡号重复");
        }
        String userName = RequestContextHolderUtil.getUserName();
        tmsOil.setUpdateTime(LocalDateTime.now());
        tmsOil.setUpdateUser(userName);
        tmsOilMapper.updateById(tmsOil);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pay(TmsOil tmsOil) throws PredictableException {
        String userName = RequestContextHolderUtil.getUserName();
        LocalDateTime now = LocalDateTime.now();
        TmsOil tmsOilDb = tmsOilMapper.selectById(tmsOil.getId());
        BigDecimal newOilAmount = null;
        if(tmsOilDb.getOilAmount() == null){
            newOilAmount = tmsOil.getOilAmount();
        }else{
            newOilAmount = tmsOilDb.getOilAmount().add(tmsOil.getOilAmount());
        }

        TmsOilRechange tmsOilRechange = new TmsOilRechange();
        tmsOilRechange.setOilBalance(tmsOilDb.getOilAmount());
        tmsOilRechange.setRechangeMoney(tmsOil.getOilAmount());
        tmsOilRechange.setOilNo(tmsOilDb.getOilNo());
        tmsOilRechange.setRechangeDate(tmsOil.getRechangeDate());
        tmsOilRechange.setIsValid(1);
        tmsOilRechange.setCreateTime(now);
        tmsOilRechange.setCreateUser(userName);

        tmsOilDb.setOilAmount(newOilAmount);
        tmsOilDb.setUpdateUser(userName);
        tmsOilDb.setUpdateTime(now);
        tmsOilMapper.updateById(tmsOilDb);
        tmsOilRechangeMapper.insert(tmsOilRechange);
    }

    @Override
    public List<TmsOil> selectList(TmsOil tmsOil) {
        List<TmsOil> result = new ArrayList<>();
        QueryWrapper<TmsOil> wrapper = new QueryWrapper<>();
        wrapper.like("oil_no", tmsOil.getOilNo());
        wrapper.eq("is_valid", 1);
        List<TmsOil> tmsOils = tmsOilMapper.selectList(wrapper);
        boolean isNew = false;
        for(TmsOil tmsOil1 : tmsOils){
            if(!tmsOil1.getOilNo().equals(tmsOil1.getOilNo())){
                isNew = true;
                break;
            }
        }
        if(tmsOils.size()<1){
            isNew = true;
        }
        if(isNew){
            result.add(tmsOil);
        }
        result.addAll(tmsOils);
        return result;
    }
}
